package dao.impl;

import core.BaseDao;
import dao.FoodDao;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.handlers.*;
import pojo.Food;
import pojo.FoodType;
import util.MyMapListHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FoodDaoImpl extends BaseDao implements FoodDao {

    /**
     * 通过条件获取所有的菜品列表
     * @param food
     * @return
     */
    @Override
    public List<Food> findFoodListByParam(Food food) {
        try {
            List<String> params = new ArrayList<>();
            sql = "SELECT * FROM food,food_type WHERE food.food_type_code=food_type.food_type_code1 AND food_is_deleted=? ";
            if (food.getFoodName() != null && !food.getFoodName().isEmpty()){
                sql += " AND food_name LIKE ? ";
                params.add("%"+food.getFoodName()+"%");
            }
            if (food.getFoodPrice() != null && food.getFoodPrice() > 0){
                sql += " AND food_price <= ? ";
                params.add(food.getFoodPrice().toString());
            }
            if(food.getFoodTypeCode() != null && !"0".equals(food.getFoodTypeCode())){
                sql += " AND food_type_code = ?";
                params.add(food.getFoodTypeCode());
            }

            sql += " ORDER BY food_id DESC LIMIT ?,?";

            Integer page = (food.getPage()-1)*food.getLimit();
            Integer limit = food.getLimit();

            List<Food> foodList = new ArrayList<>();
            if(params.size() == 3){
                List<Map<String, Object>> query = queryRunner.query(sql, new MyMapListHandler(), food.getFoodIsDeleted(), params.get(0), params.get(1), params.get(2), page, limit);
                for (Map<String, Object> map : query) {
                    Food food1 = new Food();
                    FoodType foodType = new FoodType();
                    BeanUtils.populate(food1, map);
                    BeanUtils.populate(foodType, map);
                    food1.setFoodType(foodType);
                    foodList.add(food1);
                }
                return foodList;
            }else if (params.size() == 2){
                List<Map<String, Object>> query = queryRunner.query(sql, new MyMapListHandler(), food.getFoodIsDeleted(), params.get(0), params.get(1), page, limit);
                for (Map<String, Object> map : query) {
                    Food food1 = new Food();
                    FoodType foodType = new FoodType();
                    BeanUtils.populate(food1, map);
                    BeanUtils.populate(foodType, map);
                    food1.setFoodType(foodType);
                    foodList.add(food1);
                }
                return foodList;
            }else if (params.size() == 1){
                List<Map<String, Object>> query = queryRunner.query(sql, new MyMapListHandler(), food.getFoodIsDeleted(), params.get(0), page, limit);
                for (Map<String, Object> map : query) {
                    Food food1 = new Food();
                    FoodType foodType = new FoodType();
                    BeanUtils.populate(food1, map);
                    BeanUtils.populate(foodType, map);
                    food1.setFoodType(foodType);
                    foodList.add(food1);
                }
                return foodList;
            }else if (params.size() == 0){
                List<Map<String, Object>> query = queryRunner.query(sql, new MyMapListHandler(), food.getFoodIsDeleted(), page, limit);
                for (Map<String, Object> map : query) {
                    Food food1 = new Food();
                    FoodType foodType = new FoodType();
                    BeanUtils.populate(food1, map);
                    BeanUtils.populate(foodType, map);
                    food1.setFoodType(foodType);
                    foodList.add(food1);
                }
                return foodList;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    /**
     * 获取所有可用的菜品类型
     * @return
     */
    @Override
    public List<FoodType> findAllFoodTypeInUse() {
        try {
            sql = "SELECT * FROM food_type WHERE food_type_status=1";
            return queryRunner.query(sql, new BeanListHandler<>(FoodType.class, rowProcessor));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }
    /**
     * 查询不参与排序的菜品id  ss  ss=>?
     * @return
     */
    @Override
    public List<Object> findNoSortFoodId() {
        try {
            sql ="select * FROM food WHERE food_price < 8";
            List<Object> food_idList = queryRunner.query(sql, new ColumnListHandler<Object>("food_id"));
            return food_idList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    /**
     * 删除菜品类型
     * @param foodTypeId
     * @return
     */
    @Override
    public Integer deleteFoodType(String foodTypeId) {
        try {
            sql = "DELETE FROM food_type WHERE food_type_id=?";
            return queryRunner.update(sql, foodTypeId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 把所有菜品类型状态都设置为0
     */
    @Override
    public void setStatusTo0(){
        try {
            sql = "UPDATE food_type SET food_type_status = 0";
            queryRunner.update(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改菜品类型状态
     * @param foodTypeId
     * @return
     */
    @Override
    public Integer changeTypeStatus(String foodTypeId) {
        try {
            sql = "UPDATE food_type SET food_type_status = 1 WHERE food_type_id = ?";
            return queryRunner.update(sql, foodTypeId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据菜品类型查找菜品
     * @param foodTypeName
     * @return
     */
    @Override
    public FoodType checkTypeName(String foodTypeName) {
        try {
            sql = "SELECT * FROM food_type WHERE food_type_name = ?";
            return queryRunner.query(sql, new BeanHandler<>(FoodType.class, rowProcessor), foodTypeName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 添加菜品类型
     * @param foodTypeName
     * @return
     */
    @Override
    public Integer addType(String foodTypeName) {
        try {
            sql = "SELECT food_type_code1 FROM food_type ORDER BY food_type_id DESC";
            Integer code = Integer.valueOf(queryRunner.query(sql, new ScalarHandler<>()).toString())+1;
            sql = "INSERT INTO food_type(food_type_code1,food_type_name) VALUES (?,?)";
            return queryRunner.update(sql, code, foodTypeName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 恢复
     * @param foodId
     * @return
     */
    @Override
    public Integer restore(String foodId) {
        try {
            sql = "UPDATE food SET food_is_deleted=0 WHERE food_id=?";
            return queryRunner.update(sql, foodId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 通过id 查找food对象
     * @param foodId
     * @return
     */
    @Override
    public Food findFoodById(String foodId) {
        try {
            sql = "SELECT * FROM food WHERE food_id = ?";
            return queryRunner.query(sql, new BeanHandler<>(Food.class, rowProcessor), foodId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new Food();
    }

    /**
     * 编辑菜品信息
     * @param food
     * @return
     */
    @Override
    public Integer edit(Food food) {
        try {
            sql = "UPDATE food SET food_name=?,food_type_code=?,food_price=?,food_img=?,food_update_time=? WHERE food_id=?";
            return queryRunner.update(sql,
                    food.getFoodName(),
                    food.getFoodTypeCode(),
                    food.getFoodPrice(),
                    food.getFoodImg(),
                    food.getFoodUpdateTime(),
                    food.getFoodId()
            );
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 添加菜品
     * @param food
     * @return
     */
    @Override
    public Integer add(Food food) {
        try {
            sql = "INSERT INTO food(food_name,food_type_code,food_price,food_img,food_create_time,food_update_time) VALUES (?,?,?,?,?,?)";
            return queryRunner.update(sql,
                    food.getFoodName(),
                    food.getFoodTypeCode(),
                    food.getFoodPrice(),
                    food.getFoodImg(),
                    food.getFoodCreateTime(),
                    food.getFoodUpdateTime()
            );
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

    /**
     * 真删
     * @param foodId
     * @return
     */
    @Override
    public Integer realDelete(String foodId) {
        try {
            sql = "DELETE FROM food WHERE food_id = ?";
            return queryRunner.update(sql, foodId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 软 删除
     * @param foodId
     * @return
     */
    @Override
    public Integer delete(String foodId) {
        try {
            sql = "UPDATE food SET food_is_deleted=1 WHERE food_id = ?";
            return queryRunner.update(sql, foodId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取所有菜品类型
     * @return
     */
    @Override
    public List<FoodType> getAllFoodType() {

        try {
            sql = "SELECT * FROM food_type";
            List<Map<String, Object>> query = queryRunner.query(sql, new MyMapListHandler());
            List<FoodType> typeList = new ArrayList<>();
            for (Map<String, Object> objectMap : query) {
                FoodType foodType = new FoodType();
                BeanUtils.populate(foodType, objectMap);
                typeList.add(foodType);
            }
            return typeList;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return new ArrayList<>();
    }

    /**
     * 通过条件获取所有的菜品列表的菜品个数
     * @param food
     * @return
     */
    @Override
    public Integer countFindFoodListByParam(Food food) {
        try {
            List<String> params = new ArrayList<>();
            sql = "SELECT count(1) FROM food,food_type WHERE food.food_type_code=food_type.food_type_code1 AND food_is_deleted=? ";
            if (food.getFoodName() != null && !food.getFoodName().isEmpty()){
                sql += " AND food_name LIKE ? ";
                params.add("%"+food.getFoodName()+"%");
            }
            if (food.getFoodPrice() != null){
                sql += " AND food_price <= ?";
                params.add(food.getFoodPrice().toString());
            }
            if(food.getFoodTypeCode() != null && !"0".equals(food.getFoodTypeCode())){
                sql += " AND food_type_code = ?";
                params.add(food.getFoodTypeCode());
            }

            if(params.size() == 3){
                return  Integer.valueOf(queryRunner.query(sql, new ScalarHandler<>(), food.getFoodIsDeleted(), params.get(0), params.get(1), params.get(2)).toString());
            }else if (params.size() == 2){
                return  Integer.valueOf(queryRunner.query(sql, new ScalarHandler<>(), food.getFoodIsDeleted(), params.get(0), params.get(1)).toString());
            }else if (params.size() == 1){
                return  Integer.valueOf(queryRunner.query(sql, new ScalarHandler<>(), food.getFoodIsDeleted(), params.get(0)).toString());
            }else if (params.size() == 0){
                return  Integer.valueOf(queryRunner.query(sql, new ScalarHandler<>(), food.getFoodIsDeleted()).toString());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
